/// <reference path="../_all.ts" />

module Application.Services {

    export class PlayerService implements Application.Interfaces.Services.IPlayerService {
        _playerClass: Application.Resources.IPlayerClass;
        _playerBanClass: Application.Resources.IPlayerClass;
        _playerLiftClass: Application.Resources.IPlayerClass;
        _playerHistoryClass: Application.Resources.IPlayerHistoryClass;

        public static $inject = [
            '$http',
            '$resource',
        ];

        constructor(public Http: ng.IHttpService, public Resource: ng.resource.IResourceService) {
            this._playerClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('players/:id'), null, {
                saveCollection: { method: 'POST', isArray: true }
            });
            this._playerBanClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('players/bans'), null, {
                saveCollection: { method: 'POST', isArray: true }
            });
            this._playerLiftClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('players/:id/unban'), null, null);
            this._playerHistoryClass = <Application.Resources.IMemberClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('players/:id/histories'), null, null);
        }

        GetPlayers(filters: any, callback: Function) {
            this._playerClass.query(filters, null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        GetPlayerHistories(player: Application.Resources.IPlayer, callback: Function) {
            this._playerHistoryClass.query({ id: player.player_id }, null,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        BanPlayers(players: Application.Resources.IPlayer[], callback: Function) {
            this._playerBanClass.saveCollection(null, players,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }

        LiftPlayerBan(player: Application.Resources.IPlayer, callback: Function) {
            this._playerLiftClass.save({ id: player.player_id }, player,
                function (data: any, h: any) {
                    callback(data, null);
                },
                function (err: any) {
                    callback(null, err);
                });
        }
    };
}